package com.dalin.demo.controller;

import com.dalin.demo.pojo.entity.Course;
import com.dalin.demo.service.CourseService;
import com.dalin.framework.util.LogUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/courses")
@CrossOrigin(origins = "*")
public class CourseController {
    private LogUtil log = LogUtil.getLogger(getClass());

    @Autowired
    private CourseService courseService;

    @GetMapping("/all")
    public List<Course> getAllCourses() {
        log.info("getAllCourses===");
        return courseService.getAllCourses();
    }
    @GetMapping("/list")
    public List<Course> listCourses(@RequestParam Map<String, Object> params) {
        log.debug("listCourses by params==="+ params);
        return courseService.listCoursesByParams(params);
    }

    @PostMapping("/add")
    public Course addCourse(@RequestBody Course course) throws Exception {
        log.debug("addCourse===");
        courseService.addCourse(course);
        return course;
    }

    @DeleteMapping("/delete/{id}")
    public String deleteCourse(@PathVariable Integer id) {
        log.debug("deleteCourse by id==="+ id);
        courseService.deleteCourse(id);
        return "success";
    }

    @PostMapping("/deleteByIds")
    public String deleteCoursesByIds(@RequestBody List<String> ids) {
        log.debug("deleteCourses by ids==="+ ids);
        courseService.deleteCourses(ids);
        return "success";
    }

    @PostMapping("/update")
    public String updateCourse(@RequestBody Course course) throws Exception {
        courseService.updateCourse(course);
        return "success";
    }
}